package com.example.qimo_weather

import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.squareup.picasso.Picasso
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class SecondActivity : AppCompatActivity() {

    private lateinit var weatherApiService: WeatherApiService

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        val cityEditText = findViewById<EditText>(R.id.cityEditText)
        val fetchButton = findViewById<Button>(R.id.fetchButton)
        val weatherTextView = findViewById<TextView>(R.id.weatherTextView)
        val weatherIconImageView = findViewById<ImageView>(R.id.weatherIconImageView)
        val temperatureTextView = findViewById<TextView>(R.id.temperatureTextView)
        val humidityTextView = findViewById<TextView>(R.id.humidityTextView)
        val descriptionTextView = findViewById<TextView>(R.id.descriptionTextView)

        val retrofit = Retrofit.Builder()
            .baseUrl("https://api.openweathermap.org/data/2.5/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        weatherApiService = retrofit.create(WeatherApiService::class.java)

        fetchButton.setOnClickListener {
            val city = cityEditText.text.toString()
            fetchWeather(city, "09b76ac2856af4c46d5ccce99ac4b8f5")
        }
    }

    private fun fetchWeather(city: String, apiKey: String) {
        weatherApiService.getWeather(city, apiKey).enqueue(object : Callback<WeatherResponse> {
            override fun onResponse(call: Call<WeatherResponse>, response: Response<WeatherResponse>) {
                if (response.isSuccessful) {
                    val weatherResponse = response.body()
                    val weather = weatherResponse?.weather?.firstOrNull()
                    val main = weatherResponse?.main

                    findViewById<TextView>(R.id.weatherTextView).text = "City: ${weatherResponse?.name}"
                    findViewById<TextView>(R.id.temperatureTextView).text = "Temperature: ${main?.temp}°C"
                    findViewById<TextView>(R.id.humidityTextView).text = "Humidity: ${main?.humidity}%"
                    findViewById<TextView>(R.id.descriptionTextView).text = "Description: ${weather?.description}"

                    weather?.let {
                        val weatherIconUrl = "https://openweathermap.org/img/wn/${it.icon}@2x.png"
                        val weatherIconImageView = findViewById<ImageView>(R.id.weatherIconImageView)
                        Picasso.get().load(weatherIconUrl).into(weatherIconImageView)
                    }
                } else {
                    Log.e("WeatherApp", "Error: ${response.message()}")
                }
            }

            override fun onFailure(call: Call<WeatherResponse>, t: Throwable) {
                Log.e("WeatherApp", "Failure: ${t.message}")
            }
        })
    }
}